package com.ttc.group2.videochat.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

	public static boolean CONNECTED = true;
	public static int TYPE_WIFI = 1;
	public static int TYPE_MOBILE = 2;
	public static int TYPE_NOT_CONNECTED = 0;

	public static int getConnectivityStatus(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
		if (null != activeNetwork) {
			if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
				return TYPE_WIFI;
			}
			if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
				return TYPE_MOBILE;
			}
		}
		return TYPE_NOT_CONNECTED;
	}

	public static boolean isCanConnect(Context context) {
		int conn = NetworkUtils.getConnectivityStatus(context);
		if (conn == NetworkUtils.TYPE_WIFI) {
			CONNECTED = true;
		} else if (conn == NetworkUtils.TYPE_MOBILE) {
			CONNECTED = true;
		} else if (conn == NetworkUtils.TYPE_NOT_CONNECTED) {
			CONNECTED = false;
		}
		return CONNECTED;
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectMgr != null) {
			NetworkInfo[] netInfos = connectMgr.getAllNetworkInfo();
			if (netInfos != null) {
				for (NetworkInfo netinfo : netInfos) {
					if (netinfo.getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
